package com.fwx.springcloud.controller;

import com.fwx.springcloud.entities.CommonResult;
import com.fwx.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.security.PrivateKey;

/**
 * @author : fengwenxiang
 * @date : 2022/6/23 0:04
 * @description：
 */
@RestController
@Slf4j
public class OrderController {
    private  static final String URL = "http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/Consumer/Payment/GetInfo/{idv}")
    public CommonResult<Payment>getInfo(@PathVariable("idv") long l){
        return restTemplate.getForObject(URL+"/Payment/getInfo/"+l,CommonResult.class);
    }
    @GetMapping("/Consumer/Payment/Insert")
    public CommonResult insert(Payment payment){
        return restTemplate.postForObject(URL+"/Payment/insert",payment,CommonResult.class);
    }
    @GetMapping("/Consumer/Payment/getEntity/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") Long id)
    {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(URL+"/Payment/getInfo/"+id,CommonResult.class);

        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();//getForObject()
        }else{
            return new CommonResult<>(444,"操作失败");
        }
    }
}
